/*
 * 
 * 
 * 
 */
package com.hboxs.asl.qcloudLVB.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.admin.BaseController;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Robot;
import com.hboxs.asl.qcloudLVB.entity.Live;
import com.hboxs.asl.qcloudLVB.service.LiveService;
import com.hboxs.asl.service.RobotService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * Controller - 直播记录
 */
@Controller("adminLiveController")
@RequestMapping("/asl-admin/live")
public class LiveController extends BaseController {

    @Resource(name = "liveServiceImpl")
    private LiveService liveService;
    @Resource(name = "robotServiceImpl")
    private RobotService robotService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "/admin/live/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Live live, RedirectAttributes redirectAttributes) {
        if (!isValid(live)) {
            return ERROR_VIEW;
        }
        liveService.save(live);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        Live live = liveService.find(id);
        model.addAttribute("live", live);
        //获取所有观看过的粉丝
        Set<Member> memberSet = live.getWatchedMembers();
        model.addAttribute("members", new ArrayList<>(memberSet));
        //获取添加进去的所有机器人数
        Set<Robot> robots = live.getWatchedRobots();
        model.addAttribute("robots", new ArrayList<>(robots));
        //获取实时观看的用户
        Set<Member> members = live.getGroup();
        model.addAttribute("watchingMembers", new ArrayList<>(members));
        return "/admin/live/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Long id, Long number, RedirectAttributes redirectAttributes) {
        Live live = liveService.find(id);
        //获取一定数量的机器人
        List<Robot> robotList = robotService.findAll();
        //总的机器人数
        int size = robotList.size();

        //获取数量为number的机器人
        List<Robot> robots = new ArrayList<>();

        //记录是否重复
        List<Integer> integerList = new ArrayList<>();
        int num = 0;
        for (int i=0; i<number; i++) {
            num = (int) (Math.random() * size);
            while(integerList.contains(num)) {
                num = (int) (Math.random() * size);
            }
            robots.add(robotList.get(num));
            integerList.add(num);
        }
        //原来直播中的机器人
        Set<Robot> robotSet = live.getWatchedRobots();
        robotSet.addAll(robots);
        live.setWatchedRobots(robotSet);
        //更新机器人
        liveService.update(live);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:edit.htm?id="+id;
    }


    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, Live.LiveStatus liveStatus, ModelMap model) {
        model.addAttribute("liveStatus", liveStatus);
        model.addAttribute("page", liveService.findPage(pageable, liveStatus, Live.Type.host));
        return "/admin/live/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        liveService.delete(ids);
        return SUCCESS_MESSAGE;
    }

    /**
     * 禁播
     */
    @RequestMapping(value = "/forbidden", method = RequestMethod.GET)
    public String forbidden(Long id, RedirectAttributes redirectAttributes) {
        if (liveService.find(id) != null) {
            Boolean b = liveService.forbidden(id);
            if (b) {
                addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
            } else {
                addFlashMessage(redirectAttributes, Message.warn("admin.message.error"));
            }
        } else {
            addFlashMessage(redirectAttributes, Message.warn("admin.common.invalid"));
        }
        return "redirect:list.htm?id=" + id;
    }

}
